import axios from 'axios';
import { Toast } from 'vant';
import { router } from '../router'
import baaseUrl from './base'
/* eslint no-console:off */

axios.defaults.timeout = 20000;
// 环境的切换
if (process.env.NODE_ENV == 'development') {    
    axios.defaults.baseURL = baaseUrl.devUrl;
} else if (process.env.NODE_ENV == 'debug') {    
    axios.defaults.baseURL = '';
} else if (process.env.NODE_ENV == 'production') {    
    axios.defaults.baseURL = baaseUrl.proUrl;
}

//http request 拦截器
axios.interceptors.request.use(
  config => {
    config.data = JSON.stringify(config.data);
    config.headers = {
      'Content-Type':'application/json; charset=utf-8',
      'authorization': localStorage.getItem("authorization") || ''
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

//响应拦截器即异常处理
axios.interceptors.response.use(response => {
    if(!response.data.IsSuccess){
      Toast(response.data.Message);
      return response;
    }else {
      return response
    }
}, err => {
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          Toast('错误请求');
          break;
        case 401:
          // if( window.location.href.indexOf('activity') > -1){
          //   let redirect =  window.location.href;
          //   sessionStorage.setItem('redirect',redirect);
          //   let href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe320359a5af696ca&redirect_uri=https://easyget.vip?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //测试
          //   //let href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2f6f4717a5418aba&redirect_uri=http://zsm.easyget.vip?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //生产
          //   //console.log(href)
          //   //console.log(window.location);
          //   //window.open(href);
          //   window.location.href = href
          //   break;
          // }
          if(sessionStorage.getItem('redirectUser') == 'redirectUser'){
            let redirect =  window.location.href;
            sessionStorage.setItem('redirect',redirect);
            let href;
            if( baaseUrl.env == 'test'){
              href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe320359a5af696ca&redirect_uri=https://easyget.vip?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //测试
            }else{
              href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2f6f4717a5418aba&redirect_uri=https://jttdshgc.com?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //生产
            }
            window.location.href = href;
            sessionStorage.removeItem('redirectUser');
            break;
          }
          if(localStorage.getItem('userType') == '1' || localStorage.getItem('userType') == 1){
            Toast('请重新登录');
            let redirect =  window.location.href;
            setTimeout(()=> {
              router.push({
                  path: '/login',
                  query:{
                    userType:localStorage.getItem('userType'),
                    redirect: redirect
                  }
              })
            },1500);
          }else{
            let redirect =  window.location.href;
            sessionStorage.setItem('redirect',redirect);
            let href;
            if(baaseUrl.env == 'test'){
              href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe320359a5af696ca&redirect_uri=https://easyget.vip?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //测试
            }else{
              href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2f6f4717a5418aba&redirect_uri=https://jttdshgc.com?redirect=activity/&response_type=code&scope=snsapi_userinfo&state=userType0#wechat_redirect' //生产
            }
            window.location.href = href;
          }
          break;
        case 403:
          Toast('拒绝访问');
          break;
        case 404:
          Toast('请求错误,未找到该资源');
          break;
        case 405:
          Toast('请求方法未允许');
          break;
        case 408:
          Toast('请求超时');
          break;
        case 500:
          Toast('服务器端出错');
          break;
        case 501:
          Toast('网络未实现');
          break;
        case 502:
          Toast('网络错误');
          break;
        case 503:
          Toast('服务不可用');
          break;
        case 504:
          Toast('网络超时');
          break;
        case 505:
          Toast('http版本不支持该请求');
          break;
        default:
          Toast(`连接错误${err.response.status}`);
      }
    } else {
      Toast('连接到服务器失败');
    }
    return Promise.resolve(err.response)
})


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function $get(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    })
    .then(response => {
      if(response)resolve(response.data);
    })
    .catch(err => {
      reject(err)
    })
  })
}

/**
 * 封装delete方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function $delete(url,params={}){
    return new Promise((resolve,reject) => {
      axios.delete(url,{
        params:params
      })
      .then(response => {
        if(response)resolve(response.data);
      })
      .catch(err => {
        reject(err)
      })
    })
  }


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

 export function $post(url,data = {}){
   return new Promise((resolve,reject) => {
     axios.post(url,data)
          .then(response => {
            if(response)resolve(response.data);
          },err => {
            reject(err)
          })
   })
 }

 /**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function $patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
         .then(response => {
          if(response)resolve(response.data);
         },err => {
           reject(err)
         })
  })
}

 /**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function $put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
         .then(response => {
          if(response)resolve(response.data);
         },err => {
           reject(err)
         })
  })
}
